/**
 * 
 */
package at.ac.tuwien.ifs.qse.se1.dao;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import at.ac.tuwien.ifs.qse.se1.bo.Professor;

/**
 * Convenient super class for Hibernate data access objects. Requires a SessionFactory to be set
 * (usually done by DI), providing a HibernateTemplate based on it to subclasses. The
 * HibernateTemplate can be accesses by using getHibernateTemplate(). The HibernateTemplate
 * implements the Template-Pattern. It simplifies Hibernate data access code, and converts checked
 * HibernateExceptions into unchecked DataAccessExceptions, following the org.springframework.dao
 * exception hierarchy.
 * @author Demolsky Markus
 */
public class HibernateProfessorDAO extends HibernateDaoSupport implements IProfessorDAO {
    // Declarations
    private static Logger log = Logger.getLogger("qse.se1.dao");

    /*
     * (non-Javadoc)
     * @see at.ac.tuwien.ifs.qse.se1.dao.IProfessorDAO#getProfessor(long)
     */
    public Professor getProfessor(String persNr) {
        try {
            return (Professor) getHibernateTemplate().get(Professor.class, persNr);
        } catch (DataAccessException e) {
            log.error("Error getProfessor", e);
            return null;
        }
    }

    /*
     * (non-Javadoc)
     * @see at.ac.tuwien.ifs.qse.se1.dao.IProfessorDAO#saveProfessor(at.ac.tuwien.ifs.qse.se1.bo.Professor)
     */
    public Professor saveProfessor(Professor professor) {
        log.info("Save professor " + professor.getFirstName() + " " + professor.getLastName()
                + " Lecture-Count: " + professor.getLectures().size());
        getHibernateTemplate().saveOrUpdate(professor);
        return professor;
    }

    /*
     * (non-Javadoc)
     * @see at.ac.tuwien.ifs.qse.se1.dao.IProfessorDAO#updateProfessor(at.ac.tuwien.ifs.qse.se1.bo.Professor)
     */
    public Professor updateProfessor(Professor professor) {
        log.info("Update professor " + professor.getFirstName() + " " + professor.getLastName());
        getHibernateTemplate().update(professor);
        return professor;
    }

    /*
     * (non-Javadoc)
     * @see at.ac.tuwien.ifs.qse.se1.dao.IProfessorDAO#deleteProfessor(long)
     */
    public boolean deleteProfessor(String persNr) {
        try {
            getHibernateTemplate().delete(getProfessor(persNr));
            log.info("Delete professor with id: " + persNr);
            return true;
        } catch (DataAccessException e) {
            log.error("Error deleteProfessor", e);
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * @see at.ac.tuwien.ifs.qse.se1.dao.IProfessorDAO#getProfessors(java.lang.String)
     */
    public List<Professor> getProfessors(String order) {
        // TODO Auto-generated method stub
        return null;
    }
}
